﻿Imports System.Data.SqlClient
Imports UTNComputers.clsNumerador
Public Class daoNumerador
    'Funcion para Traer el el proximo nuemerador para una tabla'
    Public Function TraeProximo(ByVal Tabla As String) As Int32
        Dim Cn As New SqlClient.SqlConnection
        Dim Cm As New SqlClient.SqlCommand
        Dim Dr As SqlClient.SqlDataReader
        Dim Numerador As New clsNumerador()

        Try
            Cn.ConnectionString = My.Resources.Conexion
            Cn.Open()
            Cm.CommandType = CommandType.StoredProcedure
            Cm.CommandText = "SP_TRAER_PROXIMO_NUMERADOR"

            Cm.Parameters.Add("@tabla", SqlDbType.VarChar)
            Cm.Parameters("@tabla").Value = Tabla.Trim.ToUpper

            Cm.Connection = Cn
            Dr = Cm.ExecuteReader
            If Dr.HasRows = True Then
                While Dr.Read
                    Numerador.Tabla = Dr.GetString(0).Trim.ToUpper
                    Numerador.Ultimo_Numero = Dr.GetInt32(1)
                End While
                Dr.Close()
                Cn.Close()
            End If
            Return Numerador.Ultimo_Numero
        Catch ex As Exception
            Throw New Exception(ex.Message)
        Finally
            Numerador.Dispose()
            Cn.Dispose()
            Cm.Dispose()
        End Try
    End Function

    '---------------------------------------------------------------------
    ' Constructor de la Clase
    '---------------------------------------------------------------------
    Public Sub New()

        MyBase.New()

    End Sub

    '---------------------------------------------------------------------
    ' Destructor de la Clase
    '---------------------------------------------------------------------
    Public Sub Dispose()

        MyBase.Finalize()

    End Sub

End Class
